package com.foreknow.web;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Department;
import com.foreknow.service.DepartmentService;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @RequestMapping(value = "/findDepartmentByPage", method = RequestMethod.POST)
    @ResponseBody
    public String findUserByPage(@RequestBody Map<String, Object> param) throws JsonProcessingException {
        //设置分页信息
        int pagenum = (int) param.get("pagenum");
        int pagesize = (int) param.get("pagesize");
        String query = (String) param.get("query");
        PageHelper.startPage(pagenum, pagesize);
        Department department = new Department();
        department.setQuery(query);
        List<Department> list = departmentService.searchInfo(department);
        PageInfo<Department> info = new PageInfo(list);
        HashMap<String, Object> hs = new HashMap<>();
        if (query != null && query != "" && list.size() > 0) {
            hs.put("data", info);
            hs.put("meta", new Constance("查询科室成功", 200));
        } else if (query != null && query != "" && list.size() == 0) {
            hs.put("meta", new Constance("查询科室失败", 201));
        } else if ((query == null || query.equals("")) && list.size() > 0) {
            hs.put("data", info);
            hs.put("meta", new Constance("获取科室列表成功", 200));
        } else if ((query == null || query.equals("")) && list.size() == 0) {
            hs.put("meta", new Constance("获取科室列表失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/insertDepartment", method = RequestMethod.POST)
    @ResponseBody
    public String insertDepartmentInfo(@RequestBody Department department) throws JsonProcessingException {
        int x = departmentService.insertDepartment(department);
        HashMap<String, Object> hs = new HashMap<>();
        if (x == 1) {
            hs.put("meta", new Constance("添加科室成功", 200));
        } else {
            hs.put("meta", new Constance("添加科室失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteDepartment", method = RequestMethod.GET)
    @ResponseBody
    public String deleteDepartmentById(@RequestParam Integer departmentId) throws JsonProcessingException {
        int x = departmentService.deleteDepartment(departmentId);
        HashMap<String, Object> hs = new HashMap<>();
        if (x == 1) {
            hs.put("meta", new Constance("删除科室成功", 200));
        } else {
            hs.put("meta", new Constance("删除科室失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/selectDepartment", method = RequestMethod.GET)
    @ResponseBody
    public String selectDepartmentById(@RequestParam Integer departmentId) throws JsonProcessingException {
        Department department = departmentService.searchDepartmentById(departmentId);
        HashMap<String, Object> hs = new HashMap<>();
        if (department != null) {
            hs.put("data", department);
            hs.put("meta", new Constance("查询科室成功", 200));
        } else {
            hs.put("meta", new Constance("查询科室失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/updateDepartment", method = RequestMethod.POST)
    @ResponseBody
    public String updateDepartmentById(@RequestBody Department department) throws JsonProcessingException {
        int x = departmentService.updateDepartment(department);
        HashMap<String, Object> hs = new HashMap<>();
        if (x == 1) {
            hs.put("meta", new Constance("修改科室成功", 200));
        } else {
            hs.put("meta", new Constance("修改科室失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteDepartmentList", method = RequestMethod.POST)
    @ResponseBody
    public String deleteDepartmentList(@RequestBody() Object[] param) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        if (param != null) {
            for (int i = 0; i < param.length; i++) {
                departmentService.deleteDepartment((Integer) param[i]);
            }
            hs.put("meta", new Constance("批量删除成功", 200));
        } else {
            hs.put("meta", new Constance("批量删除失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
}